import { Map as OlMap } from '../map/Map';

/**
 * 扩展基类
 */
export abstract class BaseExtension {
  protected map: OlMap;
  protected name: string;
  protected options: any;

  constructor(map: OlMap, name: string, options?: any) {
    this.map = map;
    this.name = name;
    this.options = options || {};
  }

  /**
   * 初始化扩展
   */
  abstract init(): void;

  /**
   * 销毁扩展
   */
  abstract destroy(): void;

  /**
   * 获取扩展名称
   */
  getName(): string {
    return this.name;
  }

  /**
   * 获取扩展配置
   */
  getOptions(): any {
    return this.options;
  }

  /**
   * 更新扩展配置
   */
  updateOptions(options: any): void {
    this.options = { ...this.options, ...options };
  }
}

/**
 * 扩展管理器类
 */
export class ExtensionManager {
  private map: OlMap;
  private extensions: Map<string, BaseExtension> = new Map();

  constructor(map: OlMap) {
    this.map = map;
  }

  /**
   * 注册扩展
   */
  register(extension: BaseExtension): void {
    const name = extension.getName();
    if (!this.extensions.has(name)) {
      this.extensions.set(name, extension);
      extension.init();
      console.log(`Extension ${name} registered`);
    }
  }

  /**
   * 获取扩展
   */
  get(name: string): BaseExtension | undefined {
    return this.extensions.get(name);
  }

  /**
   * 卸载扩展
   */
  unregister(name: string): void {
    const extension = this.extensions.get(name);
    if (extension) {
      extension.destroy();
      this.extensions.delete(name);
      console.log(`Extension ${name} unregistered`);
    }
  }

  /**
   * 卸载所有扩展
   */
  unregisterAll(): void {
    this.extensions.forEach((extension) => {
      extension.destroy();
    });
    this.extensions.clear();
  }

  /**
   * 获取所有扩展
   */
  getAll(): Map<string, BaseExtension> {
    return new Map(this.extensions);
  }

  /**
   * 销毁扩展管理器
   */
  destroy(): void {
    this.unregisterAll();
  }
}

/**
 * 示例扩展：测量工具集成
 */
export class MeasurementExtension extends BaseExtension {
  private measureTool: any;
  private isActive: boolean = false;

  constructor(map: OlMap, options?: any) {
    super(map, 'measurement', options);
  }

  init(): void {
    // 这里可以初始化测量工具
    this.map.on('measurement:start', () => {
      this.isActive = true;
    });

    this.map.on('measurement:end', () => {
      this.isActive = false;
    });

    console.log('Measurement extension initialized');
  }

  destroy(): void {
    // 销毁测量工具
    if (this.measureTool) {
      this.measureTool.destroy();
      this.measureTool = null;
    }
    
    this.map.off('measurement:start');
    this.map.off('measurement:end');
    console.log('Measurement extension destroyed');
  }

  // 自定义测量方法
  startMeasurement(type: 'distance' | 'area'): void {
    this.map.emit('measurement:start', type);
  }

  stopMeasurement(): void {
    this.map.emit('measurement:end');
  }
}

/**
 * 示例扩展：路线规划集成
 */
export class RouteExtension extends BaseExtension {
  private routeService: any;

  constructor(map: OlMap, options?: any) {
    super(map, 'route', options);
  }

  init(): void {
    // 初始化路线规划服务
    console.log('Route extension initialized');
  }

  destroy(): void {
    // 销毁路线规划服务
    if (this.routeService) {
      this.routeService.destroy();
      this.routeService = null;
    }
    console.log('Route extension destroyed');
  }

  // 自定义路线规划方法
  planRoute(start: [number, number], end: [number, number]): Promise<any> {
    // 实现路线规划逻辑
    return new Promise((resolve) => {
      setTimeout(() => {
        resolve({ route: [], distance: 10, duration: 5 });
      }, 1000);
    });
  }
}
